const axios = require('axios');

function getAccessToken(appId, appSecret) {
  const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`;
  return  new Promise((resolve, reject) => { 
    axios.get(url)
      .then(response => {
        if (response.data && response.data.access_token) {
          console.log('获取access_token成功:', response.data.access_token);
         // return response.data.access_token;
          resolve(response.data.access_token);
        } else {
          console.error('获取access_token失败:', response.data);
          // throw new Error('无法获取access_token');
          reject('无法获取access_token');
        }
      })
      .catch(error => {
        console.error('请求access_token时发生错误:', error);
        reject(error);
        // throw error;
      });
  })
}

// 替换为你的AppID和AppSecret
const appId = 'wx70a595fc42f347f0';
const appSecret = '0bbd4915cea848deca3d2c2e4264ffe9';

getAccessToken(appId, appSecret)
  .then(accessToken => {
    // 使用access_token进行后续操作
  })
  .catch(error => {
    console.error('无法获取access_token:', error);
  });